<template>
  <div>
    <el-form ref="formref" :model="form" :rules="formrules">
      <el-form-item label="部门名称" label-width="120px" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" label-width="120px" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" label-width="120px" prop="manager">
        <el-select v-model="form.manager">
          <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" label-width="120px" prop="introduce">
        <el-input v-model="form.introduce" placeholder="1-300个字符" type="textarea" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="Cancel">取 消</el-button>
      <el-button type="primary" @click="hsubmit">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { getEmployeeSimple, addDepartments, getDepartmentById, updateDepartments } from '@/api/departments'
export default {
  props: {
    pid: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // -------------------------------------------------------------自定义校验部门编号
    const validatorCode = (rule, value, callback) => {
      // console.log(value)
      let originCode = this.originList.map(item => item.code)
      if (this.isEdit) {
        // this.pid是当前部门编号,父亲传过来的
        // 把当前部门排除在外item.id是自己的部门编号(子组件这边pid和id是反的)
        originCode = this.originList.filter(item => item.id !== this.pid).map(item => item.code)
      }
      console.log(originCode)
      if (originCode.includes(value)) {
        callback(new Error(value + '部门编号被占用了'))
      } else {
        callback()
      }
    }
    // --------------------------------------------------------------------自定义部门不同级校验
    const validatorName = (rule, value, callback) => {
      // 当前部门被点击的id是pid
      // 当前部门的pid!==id
      let NameList = this.originList.filter(item => item.pid === this.pid).map(item => item.name)
      // ------------------------------------------------------------------------自定义部门同级不重复校验
      if (this.isEdit) {
        const item = this.originList.find(item => item.id === this.pid)
        const pid = item.pid
        NameList = this.originList.filter(item => item.pid === pid && item.id !== this.pid).map(item => item.name)
      }
      if (NameList.includes(value)) {
        callback(new Error(value + '部门名字被占用了'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      formrules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validatorName, trigger: 'blur' }

        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validatorCode, trigger: 'blur' }
        ],
        // change 改变了后
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      list: []
    }
  },
  created() {
    this.loadEmployee()
    // 如果是编辑，就发请求拿导部门的信息
    if (this.isEdit) {
      this.loadDepartmentById()
    }
  },
  methods: {
    async loadEmployee() {
      const res = await getEmployeeSimple()
      console.log(res)
      this.list = res.data
    },
    // -------------------------------------------------------发送请求拿到部门信息
    async loadDepartmentById() {
      const res = await getDepartmentById(this.pid)
      // 数据回填
      console.log('数据回填', res)
      this.form = res.data
    },
    // ------------------------------------------------------------做添加
    async  doAdd() {
      try {
        await addDepartments({ ...this.form, pid: this.pid })
        this.$emit('success')
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    // ---------------------------------------------------------------做编辑
    async doEdit() {
      try {
        await updateDepartments(this.form)
        // 通知父组件编辑成功
        this.$emit('success')
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    // -------------------------------------------------------------判断编辑还是添加
    hsubmit() {
      // ------------------------------------------------------------兜底校验
      this.$refs.formref.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })

      // if (this.isEdit) {
      //   this.doEdit()
      //   this.$message.success('编辑成功')
      // } else {
      //   this.doAdd()
      // }
    },
    // -------------------------------------------------------------------关闭弹层
    Cancel() {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
